package com.rainner.admin.action;

import java.sql.SQLException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.admin.action.base.EasyuiAction;
import com.rainner.admin.defined.ImageDefine;
import com.rainner.admin.defined.PermissionDefined;
import com.rainner.admin.defined.SystemDefine;
import com.rainner.admin.javabean.BookBean;
import com.rainner.admin.server.BookServer;
import com.rainner.admin.server.RolePermissionServer;

@Results({ @Result(name = "BOOKINIT", type = "freemarker", location = "/WEB-INF/content/book/index.ftl") })
public class BookAction extends EasyuiAction {
	private static final long serialVersionUID = 1L;

	private final String base = "/book/manage";
	private String type;
	private String name;
	private String value;
	private BookServer bookServer = new BookServer();
	private BookBean bookBean = new BookBean();
	private RolePermissionServer permissionServer = new RolePermissionServer();

	@Actions({ @Action(base), @Action(base + "/{type}") })
	public String manageBook() throws Exception {
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		if (type == null && username != null) {
			return "BOOKINIT";
		} else if (username == null) {
			return null;
		}
		if (SystemDefine.BOOK_ADD.equals(type) && permissionServer.checkPermission(PermissionDefined.ADD_BOOK, username.toString())) {
			bookBean.setManagerName(username.toString());
			bookBean.setBookImg(ImageDefine.imageDir);
			if (bookServer.addBookData(bookBean)) {
				this.setResult(true, "新增图书操作成功");
			} else {
				this.setResult(false, "新增图书操作失败");
			}
			map.put("message", this.result);
		} else if (SystemDefine.BOOK_DELETE.equals(type) && permissionServer.checkPermission(PermissionDefined.DELETE_BOOK, username.toString())) {
			bookServer.delBookData(bookBean);
		} else if (SystemDefine.BOOK_INIT.equals(type) && permissionServer.checkPermission(PermissionDefined.SEARCH_BOOK, username.toString())) {
			map.put("total", bookServer.countBookData());
			map.put("rows", bookServer.allBookData(rows, page));
		} else if (SystemDefine.BOOK_UPDATE.equals(type) && permissionServer.checkPermission(PermissionDefined.UPDATE_BOOK, username.toString())) {
			bookBean.setManagerName(username.toString());
			bookBean.setBookImg(ImageDefine.imageDir);
			if (bookServer.updateBookData(bookBean)) {
				this.setResult(true, "修改图书操作成功");
			} else {
				this.setResult(true, "修改图书操作失败");
			}
			map.put("message", this.result);
		}
		return MAP;
	}

	@Action("/booksearch" + "/{name}/{value}")
	public String booksearchByValue() throws SQLException{
		map.put("total", bookServer.countBookData(name,value));
		map.put("rows", bookServer.allBookData(name,value,rows, page));
		return MAP;
	}
	public void setType(String type) {
		this.type = type;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public BookBean getBookBean() {
		return bookBean;
	}

	public void setBookBean(BookBean bookBean) {
		this.bookBean = bookBean;
	}

}
